home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Developer Kit / QuickTime 2.0 Developer Kit.iso / mac / MAC / Programming Stuff / Sample Code / Codecs / examplecodec / examplecodec.c next >
Encoding:
C/C++ Source or Header  |  1993-06-23  |  35.7 KB  |  1,307 lines  |  [TEXT/MPS ]

  1. /*
  2.     This is an example of am image compression codec that handles both
  3.     compression and decompression of images as passed to it by the 
  4.     Image Compression manager. It is built as a Component Manager Component.
  5.  
  6.  
  7.     The compression scheme here is 411 YUV. The image is stored as separate 
  8.     luminance and chrominance channels. For each 2x2 block of pixels in the
  9.     source image we store 4 luminance (Y) components, 1 Y-Red component (U) and
  10.     1 Y-Blue (V) component. Each Y-component is stored as 6-bits,  resulting in a 
  11.     savings of 2.4:1 over a 24-bit/pixel image (6*4 + 2*8)/4 = 10 bits/pixel.
  12.  
  13. */
  14.  
  15.  
  16. #include <Memory.h>
  17. #include <Resources.h>
  18. #include <QuickDraw.h>    
  19. #include <QDOffscreen.h>
  20. #include <OSUtils.h>
  21. #include <SysEqu.h>
  22. #include <Errors.h>
  23. #include <FixMath.h>
  24.  
  25. #include "ImageCodec.h"
  26.  
  27.  
  28.  
  29. /* Version information */
  30.  
  31. #define    EXAMPLE_CODEC_REV            1
  32. #define    codecInterfaceVersion        1                /* high word returned in component GetVersion */
  33.  
  34.  
  35.  
  36. /* Some useful macros and constants */
  37.  
  38.  
  39. #define    R_W    0x4ccd
  40. #define    G_W    0x970a
  41. #define    B_W    0x1c29
  42. #define    PIN(_n)        ((_n) < 0 ? 0 : (_n) > 255 ? 255 : (_n))
  43.  
  44.  
  45. /*
  46.     Our data structure declarations
  47. */
  48.  
  49.  
  50.  
  51. /* This is the structure we use to hold data used by all instances of
  52.    this compressor and decompressor */
  53.  
  54. typedef struct    {                    
  55.     Handle    rgbwTable;                    /* optional encode/decode table */
  56.     Handle    giwTable;                    /* another optional encode/decode table */
  57.     CodecInfo    **info;                    /* our cached codec info structure */
  58. } SharedGlobals;
  59.  
  60.  
  61. /* This is the structure we use to store our global data for each instance */
  62.  
  63. typedef struct    {                        
  64.     SharedGlobals    *sharedGlob;        /* pointer to instance-shared globals */
  65. } Globals;
  66.  
  67.  
  68. /* Function prototypes to keep the compiler smiling. */
  69.  
  70. pascal ComponentResult
  71. EXAMPLECODEC(ComponentParameters *params,char **storage);
  72.  
  73. pascal ComponentResult
  74. OpenCodec(ComponentInstance self);
  75.  
  76. pascal ComponentResult
  77. CloseCodec(Handle storage,ComponentInstance self);
  78.  
  79. pascal ComponentResult
  80. CanDoSelector(short selector);
  81.  
  82. pascal ComponentResult 
  83. GetVersion();
  84.  
  85. pascal void
  86. CompressStrip(char *data,char *baseAddr,short rowBytes,short w,SharedGlobals *sg);
  87.  
  88. pascal void
  89. DecompressStrip(char *data,char *baseAddr,short rowBytes,short w,SharedGlobals *sg);
  90.  
  91.  
  92.  
  93. /************************************************************************************ 
  94.  *    This is the main dispatcher for our codec. All calls from the codec manager
  95.  *    will come through here, with a unique selector and corresponding parameter block.
  96.  *
  97.  *    This routine must be first in the code segment of the codec thing.
  98.  */
  99.  
  100. pascal ComponentResult
  101. EXAMPLECODEC(ComponentParameters *params,char **storage)
  102. {
  103.     /*    If the selector is less than zero, it's a Component manager selector.    */
  104.     
  105.     if ( params->what < 0  ) { 
  106.         switch ( params->what ) {
  107.         case kComponentOpenSelect:
  108.             return CallComponentFunction(params, (ComponentFunction) OpenCodec );
  109.  
  110.         case    kComponentCloseSelect:
  111.             return CallComponentFunctionWithStorage(storage,params,(ComponentFunction) CloseCodec );
  112.             
  113.         case    kComponentCanDoSelect:
  114.             return CallComponentFunction(params, (ComponentFunction) CanDoSelector);
  115.  
  116.         case kComponentVersionSelect : 
  117.             return CallComponentFunction(params, (ComponentFunction) GetVersion);
  118.  
  119.         default :
  120.             return (paramErr);
  121.         }
  122.     }
  123.     
  124.     /*
  125.      *    Here we dispatch the rest of our calls. We use the magic thing manager routine which
  126.      *    calls our subroutines with the proper parameters. The prototypes are in Image Codec.h.
  127.      */
  128.     
  129.     switch ( params->what ) {
  130.     case codecPreCompress:    
  131.         return CallComponentFunctionWithStorage(storage,params,(ComponentFunction)CDPreCompress);
  132.         
  133.     case codecBandCompress:
  134.         return CallComponentFunctionWithStorage(storage,params,(ComponentFunction)CDBandCompress);
  135.         
  136.     case codecPreDecompress:
  137.         return CallComponentFunctionWithStorage(storage,params,(ComponentFunction)CDPreDecompress);
  138.  
  139.     case codecBandDecompress:
  140.         return CallComponentFunctionWithStorage(storage,params,(ComponentFunction)CDBandDecompress);
  141.  
  142.     case codecCDSequenceBusy:
  143.         return 0;                    /* our codec is never asynchronously busy */
  144.  
  145.     case codecGetCodecInfo:
  146.         return CallComponentFunctionWithStorage(storage,params,(ComponentFunction)CDGetCodecInfo);
  147.         
  148.     case codecGetCompressedImageSize:
  149.         return CallComponentFunctionWithStorage(storage,params,(ComponentFunction)CDGetCompressedImageSize);
  150.  
  151.     case codecGetMaxCompressionSize:
  152.         return CallComponentFunctionWithStorage(storage,params,(ComponentFunction)CDGetMaxCompressionSize);
  153.  
  154.     case codecGetCompressionTime:
  155.         return CallComponentFunctionWithStorage(storage,params,(ComponentFunction)CDGetCompressionTime);
  156.  
  157.     case codecGetSimilarity:
  158.         return CallComponentFunctionWithStorage(storage,params,(ComponentFunction)CDGetSimilarity);
  159.  
  160.     case codecTrimImage:
  161.         return CallComponentFunctionWithStorage(storage,params,(ComponentFunction)CDTrimImage);
  162.     
  163.     default:
  164.         return(paramErr);
  165.     }    
  166. }
  167.  
  168. /************************************************************************************ 
  169.  *    This gets called when the thing instance is opened. We allocate our storage at this
  170.  *    point. If we have shared globals, we check if they exist, and put a pointer to them 
  171.  *    in our instance globals so that other calls can get to them.
  172.  */
  173.  
  174. pascal ComponentResult
  175. OpenCodec(ComponentInstance self)
  176. {
  177.     ComponentResult result;
  178.     Globals             **glob;
  179.     
  180.     /* 
  181.         First we allocate shared storage. This should be a handle to any
  182.         kind of data used by the thing instance. They should be allocated
  183.         in the current heap.
  184.     
  185.     */     
  186.          
  187.     if ( (glob = (Globals **)NewHandleClear(sizeof(Globals))) == nil )  {
  188.         return(MemError());
  189.     }
  190.     SetComponentInstanceStorage(self,(Handle)glob);
  191.     
  192.     /*     Check and initialize our shared globals */
  193.     
  194.     result = InitSharedTables(glob,self);
  195.     
  196.     return result;
  197. }
  198.  
  199. /************************************************************************************ 
  200.  *    This gets called when the thing instance is opened. We allocate our shared storage at this
  201.  *    point. 
  202.  
  203.  *    If we have shared globals, we check if they exist, otherwise we allocate
  204.  *  them and set the ComponentRefCon so that other instances can use them.
  205.  *
  206.  *    The shared globals hold our CodecInfo struct, which we read from our resource file,
  207.  *  and some tables that we use for speed. If we cant get the tables we can work without
  208.  *  them. All items in the shared globals are made purgeable when the last of our 
  209.  *    instances is closed. If our component was loaded in the application heap ( because
  210.  *    there was no room in the system heap) then we keep our shared storage in the app heap.
  211.  *
  212.  *  We keep a pointer to the shared globals in our instance globals so that other calls can get to them.
  213.  */
  214.  
  215.  
  216. ComponentResult
  217. InitSharedTables(Globals **glob,ComponentInstance self)
  218. {
  219.     SharedGlobals    *sGlob;
  220.     long            i,j,*lp;
  221.     char            *cp;
  222.     short            resFile;
  223.     THz                saveZone;
  224.     Boolean            inAppHeap;
  225.     OSErr            result = noErr;
  226.         
  227.      
  228.     saveZone = GetZone();
  229.     inAppHeap = ( GetComponentInstanceA5(self) != 0 );
  230.     if ( !inAppHeap )
  231.         SetZone(SystemZone());
  232.     if ( (sGlob=(SharedGlobals*)GetComponentRefcon((Component)self)) == nil  ) {
  233.         if ( (sGlob = (SharedGlobals*)NewPtrClear(sizeof(SharedGlobals))) == nil ) { 
  234.             result = MemError();
  235.             goto obail;
  236.         } 
  237.         SetComponentRefcon((Component)self,(long)sGlob);
  238.     }
  239.  
  240.     (*glob)->sharedGlob = sGlob;    // keep this around where it's easy to get at
  241.     
  242.  
  243.     if ( sGlob->info == nil || *(Handle)sGlob->info == nil  )  {
  244.  
  245.         if ( sGlob->info ) 
  246.             DisposHandle((Handle)sGlob->info);
  247.  
  248.         /* Get the CodecInfo struct which we keep in our resource fork */
  249.         
  250.         resFile = OpenComponentResFile((Component)self);
  251.         if (resFile == -1) {
  252.             result = memFullErr;
  253.             goto obail;
  254.         }
  255.         sGlob->info = (CodecInfo **) Get1Resource(codecInfoResourceType,128);
  256.         if ( sGlob->info == nil ) {
  257.             CloseComponentResFile(resFile);
  258.             result = ResError();
  259.             goto obail;
  260.         }
  261.         LoadResource((Handle)sGlob->info);
  262.         if ( ResError() ) {
  263.             CloseComponentResFile(resFile);
  264.             result = ResError();
  265.             goto obail;
  266.         }
  267.         DetachResource((Handle)sGlob->info);
  268.         CloseComponentResFile(resFile);
  269.     }
  270.     HNoPurge((Handle)sGlob->info);
  271.     
  272.     if ( sGlob->rgbwTable == nil || *sGlob->rgbwTable == nil )  {
  273.         if ( sGlob->rgbwTable )
  274.             ReallocHandle(sGlob->rgbwTable,3*256*sizeof(long));
  275.         else 
  276.             sGlob->rgbwTable = NewHandleSys(3*256*sizeof(long));
  277.             
  278.         /* we can actual still work without these tables, so we dont bail
  279.            if we cant get the memory for them */
  280.            
  281.         if ( sGlob->rgbwTable  && *sGlob->rgbwTable ) {
  282.             lp = (long *)*sGlob->rgbwTable;
  283.             for ( i=0, j = 0; i < 256; i++, j += R_W )
  284.                 *lp++ = j;
  285.             for ( i=0, j = 0; i < 256; i++, j += G_W )
  286.                 *lp++ = j;
  287.             for ( i=0, j = 0; i < 256; i++, j += B_W )
  288.                 *lp++ = j;
  289.         }
  290.     }
  291.     if ( sGlob->rgbwTable ) 
  292.         HNoPurge(sGlob->rgbwTable);            /* make sure it wont get purged while we are open */
  293.     
  294.     /* green inverse table */
  295.  
  296.     if ( sGlob->giwTable == nil || *sGlob->giwTable == nil  )  {
  297.         if ( sGlob->giwTable ) 
  298.             ReallocHandle(sGlob->giwTable,256);
  299.         else 
  300.             sGlob->giwTable = NewHandleSys(256);
  301.  
  302.         /* we can actual still work without these tables, so we dont bail
  303.            if we cant get hte memory for them */
  304.  
  305.         if ( sGlob->giwTable && *sGlob->giwTable  ) {
  306.             for ( i=0, cp = *sGlob->giwTable ; i < 256; i++ )
  307.                 *cp++ = PIN( (i<<16) / G_W);
  308.         }
  309.     }
  310.     if ( sGlob->giwTable ) 
  311.         HNoPurge(sGlob->giwTable);            /* make sure it wont get purged while we are open */
  312.  
  313.  
  314. obail:
  315.  
  316.     SetZone(saveZone);
  317.     if ( result != noErr && sGlob != nil ) {
  318.         if ( sGlob->rgbwTable )
  319.             DisposHandle(sGlob->rgbwTable);
  320.         if ( sGlob->info )
  321.             DisposHandle((Handle)sGlob->info);
  322.         DisposPtr((Ptr)sGlob);
  323.         SetComponentRefcon((Component)self,(long)nil);
  324.     }
  325.     return(result);
  326. }
  327.  
  328. /************************************************************************************ 
  329.  *    This gets called when the thing instance is closed. We need to get rid of any 
  330.  *    instance storage here. 
  331.  */
  332.  
  333. pascal ComponentResult
  334. CloseCodec(Handle storage,ComponentInstance self)
  335. {
  336.     SharedGlobals    *sGlob;
  337.     long            i;
  338.     Globals            **glob = (Globals **)storage;
  339.         
  340.     /*    If we are closing our last instance 
  341.         then we make the shared global items purgeable to
  342.         speed things up next instance.
  343.      */
  344.     
  345.     if ( CountComponentInstances((Component)self) == 1) {
  346.         if ( (sGlob=(SharedGlobals*)(*glob)->sharedGlob) != nil  ) {
  347.             if ( sGlob->rgbwTable )
  348.                 HPurge(sGlob->rgbwTable);
  349.             if ( sGlob->giwTable )
  350.                 HPurge(sGlob->giwTable);
  351.             if ( sGlob->info )
  352.                 HPurge((Handle)sGlob->info);
  353.         }
  354.     }
  355.     if ( glob )    
  356.         DisposHandle((Handle)glob);
  357.     return(noErr);
  358. }
  359.  
  360.  
  361.  
  362. /************************************************************************************ 
  363.  *     Return true if we can handle the selector, otherwise false.
  364.  */
  365.  
  366. pascal ComponentResult
  367. CanDoSelector(short selector)
  368. {    
  369.     switch(selector) {
  370.         case kComponentOpenSelect:
  371.         case kComponentCloseSelect:
  372.         case kComponentCanDoSelect:
  373.         case kComponentVersionSelect : 
  374.         case codecPreCompress:
  375.         case codecBandCompress:
  376.         case codecPreDecompress:
  377.         case codecBandDecompress:
  378.         case codecCDSequenceBusy:
  379.         case codecGetCodecInfo:
  380.         case codecGetCompressedImageSize:
  381.         case codecGetMaxCompressionSize:
  382.         case codecGetCompressionTime:
  383.         case codecGetSimilarity:
  384.         case codecTrimImage:
  385.             return(true);
  386.         default:
  387.             return (false);
  388.     }
  389. }
  390.  
  391.  
  392. /************************************************************************************ 
  393.  *    Return the version of this component ( defines interface ) and revision level
  394.  *    of the code.
  395.  */
  396.  
  397. pascal ComponentResult 
  398. GetVersion()
  399. {
  400.     return ((codecInterfaceVersion<<16) | EXAMPLE_CODEC_REV);        /* interface version in hi word, code rev in lo word  */
  401. }
  402.  
  403.  
  404. /************************************************************************************ 
  405.  *    CDPreCompress gets called before an image is compressed, or whenever the source pixmap
  406.  *    pixel size changes when compressing a sequence. We return information about
  407.  *    how we can compress the image to the codec manager, so that it can fit the source data
  408.  *    to our requirements. The ImageDescriptor is already filled in, so we can use it for 
  409.  *    reference (or even add to it ). PixelSize is the pixel depth of the source pixmap, we
  410.  *    use this as a reference for deciding what we can do. The other parameters return information
  411.  *    to the CodecManager about what we can do. We can also do setup here if we want to.
  412.  */
  413.  
  414. pascal long
  415. CDPreCompress(Handle storage,register CodecCompressParams *p)
  416. {
  417.     CodecCapabilities *capabilities = p->capabilities;
  418.  
  419.     /*
  420.      *    First we return which depth input pixels we can deal with - based on what the
  421.      *    app has available - we can only work with 32 bit input pixels.
  422.      */
  423.        
  424.     switch ( (*p->imageDescription)->depth )  {
  425.         case 16:
  426.             capabilities->wantedPixelSize = 32;
  427.             break;
  428.         default:
  429.             return(codecConditionErr);
  430.             break;
  431.     }
  432.  
  433.     /* if the buffer gets banded,  return the smallest one we can deal with */
  434.     
  435.     capabilities->bandMin = 2;
  436.  
  437.     /* if the buffer gets banded, return the increment it be should grown */
  438.  
  439.     capabilities->bandInc = 2;
  440.     
  441.     
  442.     /*
  443.      *    If a codec needs the dimensions of the source pixmap to be of certain multiples
  444.      *    it can ask for the image to be extended out (via pixel replication) vertically
  445.      *    and/or horizontally.
  446.      *
  447.      *    In our case, we're dealing with 2 by 2 blocks and therefore we want the image
  448.      *    height and width to both be multiples of 2.  If either dimension is odd, we
  449.      *    ask it have it extended by one pixel.
  450.      */
  451.  
  452.     capabilities->extendWidth = (*p->imageDescription)->width & 1;
  453.     capabilities->extendHeight = (*p->imageDescription)->height & 1;
  454.     
  455.     return(noErr);
  456. }
  457.  
  458.  
  459. /************************************************************************************ 
  460.  *    CDBandCompress gets called when the codec manager wants us to compress an image, or a horizontal 
  461.  *    band of an image. The pixel data at sBaseAddr is guaranteed to conform to the criteria we 
  462.  *    demanded in BeginCompress.
  463.  */
  464.  
  465. pascal long
  466. CDBandCompress(Handle storage,register CodecCompressParams *p)
  467. {
  468.     short                width,height;
  469.     Ptr                    cDataPtr,dataStart;
  470.     short                depth;
  471.     Rect                sRect;
  472.     long                offsetH,offsetV;
  473.     Globals                **glob  = (Globals **)storage;
  474.     register char         *baseAddr;
  475.     long                numLines,numStrips;
  476.     short                rowBytes;
  477.     long                stripBytes;
  478.     char                mmuMode = 1;
  479.     register short        y;
  480.     ImageDescription    **desc = p->imageDescription;
  481.     OSErr                result = noErr;
  482.     
  483.     /*    If there is a progress proc, give it an open call at the start of this band. */
  484.  
  485.     if (p->progressProcRecord.progressProc)
  486.         p->progressProcRecord.progressProc(codecProgressOpen,0,
  487.             p->progressProcRecord.progressRefCon);
  488.  
  489.     width = (*desc)->width;
  490.     height = (*desc)->height;
  491.     depth = (*desc)->depth;
  492.     dataStart = cDataPtr = p->data;
  493.  
  494.     /* figure out offset to first pixel in baseAddr from the pixelsize and bounds */
  495.  
  496.     rowBytes = p->srcPixMap.rowBytes & 0x3fff;
  497.     sRect =  p->srcPixMap.bounds;
  498.     numLines = p->stopLine - p->startLine;        /* number of scanlines in this band */
  499.     numStrips = (numLines+1)>>1;                /* number of strips in this band */
  500.     stripBytes = ((width+1)>>1) * 5;
  501.     
  502.     /* adjust the source baseAddress to be at the beginning of the desired rect */
  503.  
  504.     switch ( p->srcPixMap.pixelSize ) {
  505.     case 32:
  506.         offsetH = sRect.left<<2;
  507.         break;
  508.     case 16:
  509.         offsetH = sRect.left<<1;
  510.         break;
  511.     case 8:
  512.         offsetH = sRect.left;
  513.         break;
  514.     default:
  515.         result = codecErr;
  516.         goto bail;
  517.     }
  518.     offsetV = sRect.top * rowBytes;
  519.     baseAddr = p->srcPixMap.baseAddr + offsetH + offsetV;
  520.  
  521.  
  522.     /* if there is not a flush proc, adjust the pointer to the next band */
  523.     
  524.     if (  p->flushProcRecord.flushProc == nil )
  525.         cDataPtr += (p->startLine>>1) * stripBytes;
  526.     else {
  527.         if ( p->bufferSize < stripBytes ) {
  528.             result = codecSpoolErr;
  529.             goto bail;
  530.         }
  531.     }
  532.  
  533.  
  534.     /* do the slower flush/progress case if we have too */
  535.     
  536.     if (  p->flushProcRecord.flushProc  || p->progressProcRecord.progressProc ) {
  537.         SharedGlobals *sg = (*glob)->sharedGlob;
  538.  
  539.  
  540.         for ( y=0; y < numStrips; y++) {
  541.             SwapMMUMode(&mmuMode);
  542.             CompressStrip(cDataPtr,baseAddr,rowBytes,width,sg);
  543.             SwapMMUMode(&mmuMode);
  544.             baseAddr += rowBytes<<1;
  545.             if ( p->flushProcRecord.flushProc ) { 
  546.                 if ( (result=p->flushProcRecord.flushProc(cDataPtr,stripBytes,
  547.                         p->flushProcRecord.flushRefCon)) != noErr) {
  548.                     result = codecSpoolErr;
  549.                     goto bail;
  550.                 }
  551.             } else {
  552.                 cDataPtr += stripBytes;
  553.             }
  554.             if (p->progressProcRecord.progressProc) {
  555.                 if ( (result=p->progressProcRecord.progressProc(codecProgressUpdatePercent,
  556.                     FixDiv(y,numStrips),p->progressProcRecord.progressRefCon)) != noErr ) {
  557.                         result = codecAbortErr;
  558.                         goto bail;
  559.                     }
  560.             }
  561.         }
  562.     } else {
  563.         SharedGlobals *sg = (*glob)->sharedGlob;
  564.         short    tRowBytes = rowBytes<<1;
  565.  
  566.         SwapMMUMode(&mmuMode);
  567.         for ( y=numStrips; y--; ) {
  568.             CompressStrip(cDataPtr,baseAddr,rowBytes,width,sg);
  569.             cDataPtr += stripBytes;
  570.             baseAddr += tRowBytes;
  571.         }
  572.         SwapMMUMode(&mmuMode);
  573.     }
  574.  
  575.     /*
  576.     
  577.         return size and similarity on the last band 
  578.         
  579.     */
  580.     
  581.     if ( p->conditionFlags & codecConditionLastBand ) {
  582.         (*p->imageDescription)->dataSize = stripBytes * ((height+1)>>1);    /* return the actual size of the compressed data */
  583.         p->similarity = 0;                            /* we don't do frame differencing */
  584.     }
  585.     
  586. bail:
  587.     /*    If there is a progress proc, give it a close call at the end of this band. */
  588.  
  589.     if (p->progressProcRecord.progressProc)
  590.         p->progressProcRecord.progressProc(codecProgressClose,0,
  591.             p->progressProcRecord.progressRefCon);
  592.         
  593.     return(result);
  594. }
  595.  
  596.  
  597.  
  598. /************************************************************************************ 
  599.  *    CDPreDecompress gets called before an image is decompressed. We return information about
  600.  *    how we can decompress the image to the codec manager, so that it can fit the destination data
  601.  *    to our requirements. 
  602.  */
  603.  
  604. pascal long
  605. CDPreDecompress(Handle storage,register CodecDecompressParams *p)
  606. {
  607.     register CodecCapabilities    *capabilities = p->capabilities;
  608.     Rect    dRect = p->srcRect;
  609.     
  610.     /*    Check if the matrix is okay for us. We don't do anything fancy. */
  611.     
  612.     if ( !TransformRect(p->matrix,&dRect,nil) )
  613.         return(codecConditionErr);
  614.  
  615.  
  616.     /*    Decide which depth compressed data we can deal with. */
  617.     
  618.     switch ( (*p->imageDescription)->depth )  {
  619.         case 16:
  620.             break;
  621.         default:
  622.             return(codecConditionErr);
  623.             break;
  624.     }
  625.     
  626.     /*    We can deal only 32 bit pixels. */
  627.  
  628.     capabilities->wantedPixelSize = 32;    
  629.     
  630.     /*    The smallest possible band we can do is 2 scan lines. */
  631.     
  632.     capabilities->bandMin = 2;
  633.  
  634.     /*    We can deal with 2 scan line high bands. */
  635.  
  636.     capabilities->bandInc = 2;
  637.     
  638.     /*    If we needed our pixels to be aligned on some integer multiple we would set these to
  639.      *    the number of pixels we need the dest extended by. If we dont care, or we take care of
  640.      *  it ourselves we set them to zero.
  641.      */
  642.  
  643.     capabilities->extendWidth = p->srcRect.right & 1;
  644.     capabilities->extendHeight = p->srcRect.bottom & 1;
  645.     
  646.     return(noErr);
  647. }
  648.  
  649.  
  650. /************************************************************************************ 
  651.  *    CDBandDecompress gets called when the codec manager wants us to decompress an image or a horizontal 
  652.  *    band of an image. The pixel data at baseAddr is guaranteed to conform to the criteria we 
  653.  *    demanded in BeginDecompress. If maskIn is true, then the mask data at mBaseAddr is valid, and
  654.  *    we need to clear bits in it that correspond to any pixels in the destination we do not want to 
  655.  *    change. ( We always write all pixels, so we dont care. This mode is important only for those
  656.  *    codecs that have frame differencing and don't always write all the pixels. )
  657.  */
  658.  
  659. pascal long
  660. CDBandDecompress(Handle storage,register CodecDecompressParams *p)
  661. {
  662.     Rect                dRect;
  663.     long                offsetH,offsetV;
  664.     Globals                **glob  = (Globals **)storage;
  665.     long                numLines,numStrips;
  666.     short                rowBytes;
  667.     long                stripBytes;
  668.     short                width;
  669.     register short        y;
  670.     register char        *baseAddr;
  671.     char                *cDataPtr;
  672.     char                mmuMode = 1;
  673.     OSErr                result = noErr;
  674.     
  675.     /*    Calculate the real base address based on the bounds rect. If it's not 
  676.         a linear transformation, we dont do it. */
  677.  
  678.     dRect = p->srcRect;
  679.     if ( !TransformRect(p->matrix,&dRect,nil) )
  680.         return(paramErr);
  681.  
  682.     /*    If there is a progress proc, give it an open call at the start of this band. */
  683.  
  684.     if (p->progressProcRecord.progressProc)
  685.         p->progressProcRecord.progressProc(codecProgressOpen,0,
  686.             p->progressProcRecord.progressRefCon);
  687.     
  688.     
  689.     /* initialize some local variables */
  690.     
  691.     width = (*p->imageDescription)->width;
  692.     rowBytes = p->dstPixMap.rowBytes;                    
  693.     numLines = p->stopLine - p->startLine;            /* number of scanlines in this band */
  694.     numStrips = (numLines+1)>>1;                    /* number of strips in this band */
  695.     stripBytes = ((width+1)>>1) * 5;                /* number of bytes in one strip of blocks */
  696.     cDataPtr = p->data;
  697.     
  698.     /* adjust the destination baseaddress to be at the beginning of the desired rect */
  699.     
  700.     offsetH = (dRect.left - p->dstPixMap.bounds.left);
  701.     switch (  p->dstPixMap.pixelSize ) {
  702.     case 32:
  703.         offsetH <<=2;                    /* 1 pixel = 4 bytes */
  704.         break;
  705.     case 16:
  706.         offsetH <<=1;                    /* 1 pixel = 2 bytes */
  707.         break;
  708.     case 8:                                
  709.         break;                            /* 1 pixel = 1 byte */
  710.     default:
  711.         result = codecErr;                /* we dont handle these cases, thow we could */
  712.         goto bail;
  713.     }
  714.     offsetV = (dRect.top - p->dstPixMap.bounds.top) * rowBytes;
  715.     baseAddr = p->dstPixMap.baseAddr + offsetH + offsetV;
  716.  
  717.  
  718.     /* 
  719.      *    If we are skipping some data, we just skip it here. We can tell because
  720.      *    firstBandInFrame says this is the first band for a new frame, and
  721.      *    if startLine is not zero, then that many lines were clipped out.
  722.      */
  723.  
  724.     if ( (p->conditionFlags & codecConditionFirstBand) && p->startLine != 0 ) {
  725.         if ( p->dataProcRecord.dataProc ) {
  726.             for ( y=0; y  < p->startLine>>1; y++ )  {
  727.                 if ( (result=p->dataProcRecord.dataProc(&cDataPtr,stripBytes,
  728.                         p->dataProcRecord.dataRefCon)) != noErr ) {
  729.                     result = codecSpoolErr;
  730.                     goto bail;
  731.                 }
  732.                 cDataPtr += stripBytes;
  733.             }
  734.         } else
  735.             cDataPtr += (p->startLine>>1) * stripBytes;
  736.     }
  737.     
  738.     /*
  739.      *    If theres a dataproc spooling the data to us, then we have to do the data
  740.      *    in whatever size chunks they want to give us, or if there is a progressProc
  741.      *  make sure to call it as we go along.
  742.      */
  743.     
  744.     if ( p->dataProcRecord.dataProc || p->progressProcRecord.progressProc ) {
  745.         SharedGlobals *sg = (*glob)->sharedGlob;
  746.     
  747.         for (y=0; y < numStrips; y++) {
  748.             if (p->dataProcRecord.dataProc) {
  749.                 if ( (result=p->dataProcRecord.dataProc(&cDataPtr,stripBytes,
  750.                         p->dataProcRecord.dataRefCon)) != noErr ) {
  751.                     result = codecSpoolErr;
  752.                     goto bail;
  753.                 }
  754.             }
  755.             SwapMMUMode(&mmuMode);
  756.             DecompressStrip(cDataPtr,baseAddr,rowBytes,width,sg);
  757.             SwapMMUMode(&mmuMode);
  758.             baseAddr += rowBytes<<1;
  759.             cDataPtr += stripBytes;
  760.             if (p->progressProcRecord.progressProc) {
  761.                 if ( (result=p->progressProcRecord.progressProc(codecProgressUpdatePercent,
  762.                     FixDiv(y, numStrips),p->progressProcRecord.progressRefCon)) != noErr ) {
  763.                     result = codecAbortErr;
  764.                      goto bail;
  765.                 }
  766.             }
  767.         }
  768.     
  769.     
  770.     /* 
  771.      *
  772.      * otherwise - do the fast case. 
  773.      *
  774.      */
  775.          
  776.     } else {
  777.         SharedGlobals *sg = (*glob)->sharedGlob;
  778.         short    tRowBytes = rowBytes<<1;
  779.         
  780.         SwapMMUMode(&mmuMode);
  781.         for ( y=numStrips; y--; ) {
  782.             DecompressStrip(cDataPtr,baseAddr,rowBytes,width,sg);
  783.             baseAddr += tRowBytes;
  784.             cDataPtr += stripBytes;
  785.         }
  786.         SwapMMUMode(&mmuMode);
  787.     }
  788.     
  789.     /* 
  790.      *
  791.      *  IMPORTANT: update pointer to data in params, so when we get the  next
  792.      *  band we'll be at the right place in our data.
  793.      *  
  794.      */
  795.     
  796.     p->data = cDataPtr;
  797.     
  798.     if ( p->conditionFlags & codecConditionLastBand ) {
  799.         /* Tie up any loose ends on the last band of the frame, if we had any */
  800.     }
  801.  
  802. bail:
  803.     /*    If there is a progress proc, give it a close call at the end of this band. */
  804.  
  805.     if (p->progressProcRecord.progressProc)
  806.         p->progressProcRecord.progressProc(codecProgressClose,0,
  807.             p->progressProcRecord.progressRefCon);
  808.     return(result);
  809. }
  810.  
  811.  
  812. /************************************************************************************ 
  813.  *    CDGetCodecInfo allows us to return information about ourselves to the codec manager.
  814.  *    
  815.  *    There will be a tool for determining appropriate values for the accuracy, speed
  816.  *    and level information. For now we estimate with scientific wild guessing.
  817.  *
  818.  *  The info is stored as a resource in the same file with our component.
  819.  */
  820.  
  821. pascal ComponentResult
  822. CDGetCodecInfo(Handle storage,CodecInfo *info)
  823. {
  824.     Globals **glob = (Globals **)storage;
  825.  
  826.     if ( info == nil ) 
  827.         return(paramErr);
  828.     BlockMove((Ptr)*((*glob)->sharedGlob)->info,(Ptr)info,sizeof(CodecInfo));
  829.     return(noErr);
  830. }
  831.  
  832.  
  833. /************************************************************************************ 
  834.  *    When CDGetSimilarity is called, we return the percent of the compressed image A that
  835.  *    is different from compressed image B. This can be used by applications that use sequence
  836.  *    dynamics as an indicator for editing image sequences.
  837.  *    
  838.  *    If the codec cannot do a direct similarity comparison, the ICM decompresses image A and
  839.  *    do a comparison with image B.  This call is provided so that a codec can save the time
  840.  *    of the intermediate decompress if it can do the comparison directly.
  841.  */
  842.  
  843. pascal ComponentResult
  844. CDGetSimilarity(Handle storage,PixMapHandle src,const Rect *srcRect,ImageDescriptionHandle desc,
  845.                 Ptr data,Fixed *similarity)
  846. {
  847. #pragma    unused(storage,src,srcRect,desc,data,dif,refcon)
  848.  
  849.     /*    This call is not implemented yet, which is okay, because its not used very much and is not mandatory */
  850.  
  851.     return(codecUnimpErr);
  852. }
  853.  
  854.  
  855. /************************************************************************************ 
  856.  *    When CDGetCompressedImageSize is called, we return the size in bytes of the given compressed
  857.  *    data ( for one image frame).
  858.  */
  859.  
  860. pascal ComponentResult
  861. CDGetCompressedImageSize(Handle storage,ImageDescriptionHandle desc,Ptr data,long dataSize,
  862.     DataProcRecordPtr dataProc,long *size)
  863. {
  864. #pragma    unused(storage,data,dataSize,dataProc)
  865.  
  866.     short    width =(*desc)->width;
  867.     short    height = (*desc)->height;
  868.     
  869.     if ( size == nil )
  870.         return(paramErr);
  871.         
  872.     /*
  873.      *    Our data has a size which is deterministic based on the image size. If it were not we
  874.      *    could encode the size in the compressed data, or figure it out by walking the
  875.      *    compressed data.
  876.      */
  877.      
  878.     *size = ((width+1)/2) * 5 * ((height+1)/2);
  879.     return(noErr);
  880. }
  881.  
  882.  
  883. /************************************************************************************ 
  884.  *    When CDGetMaxCompressionSize is called, we return the maximum size the compressed data for
  885.  *    the given image would be in bytes.
  886.  */
  887.  
  888. pascal ComponentResult
  889. CDGetMaxCompressionSize(Handle storage,PixMapHandle src,const Rect *srcRect,short depth,
  890.     CodecQ quality,long *size)
  891. {
  892. #pragma    unused(storage,src,depth,quality)
  893.     
  894.     short width = srcRect->right - srcRect->left;
  895.     short height = srcRect->bottom - srcRect->top;
  896.  
  897.     /*    we always end up with a fixed size. If we did not, we would return the worst case size */
  898.     
  899.     *size = ((width+1)/2) * 5 * ((height+1)/2);    
  900.  
  901.     return(noErr);
  902. }
  903.  
  904.  
  905. /************************************************************************************ 
  906.  *    When CDGetCompressionTime is called, we return the approximate time for compressing
  907.  *    the given image would be in milliseconds. We also return the closest actual quality
  908.  *    we can handle for the requested value.
  909.  */
  910.  
  911. pascal ComponentResult
  912. CDGetCompressionTime(Handle storage,PixMapHandle src,const Rect *srcRect,short depth,
  913.         CodecQ *spatialQuality,CodecQ *temporalQuality,unsigned long *time)
  914. {
  915. #pragma    unused(storage,src,srcRect,depth)
  916.  
  917.     if (time)
  918.         *time = 0;                                    /* we don't know how many msecs */
  919.  
  920.     if (spatialQuality)
  921.         *spatialQuality = codecNormalQuality;        /* we have only one quality level for now */
  922.     
  923.     if (temporalQuality)
  924.         *temporalQuality = 0;                        /* we cannot do temporal compression */
  925.  
  926.     return(noErr);
  927. }
  928.  
  929.  
  930. /************************************************************************************ 
  931.  *    When CDTrimImage is called, we take the given compressed data and return only the portion
  932.  *    which is represented by the trimRect. We can return a little more if we have too, but we
  933.  *    need only return enough so that the image in trimRect is properly displayed. We then
  934.  *    adjust the rectangle to corresond to the same rectangle in the new trimmed data.
  935.  */
  936.  
  937. pascal ComponentResult
  938. CDTrimImage(Handle storage,ImageDescriptionHandle desc,Ptr inData,long inDataSize,
  939.         DataProcRecordPtr dataProc,Ptr outData,long outDataSize,FlushProcRecordPtr flushProc,
  940.         Rect *trimRect,ProgressProcRecordPtr progressProc)
  941. {
  942. #pragma    unused(storage)
  943.  
  944.     Rect    rect = *trimRect;
  945.     char    *dataP,*odP,*startP;
  946.     short    trimOffTop;
  947.     short    trimOffBottom;
  948.     short    trimOffLeft;
  949.     short    trimOffRight;
  950.     short    bytesOffLeft;
  951.     short    newHeight,newWidth;
  952.     long    size;
  953.     short    stripBytes;
  954.     short    newStripBytes;
  955.     short    i,y;
  956.     OSErr    result = noErr;
  957.     
  958.         
  959.     if ( dataProc->dataProc == nil )
  960.         dataProc = nil;
  961.     if ( flushProc->flushProc == nil )
  962.         flushProc = nil;
  963.     if ( progressProc->progressProc == nil )
  964.         progressProc = nil;
  965.     if ( progressProc ) 
  966.         progressProc->progressProc(codecProgressOpen,0,progressProc->progressRefCon);
  967.  
  968.     dataP = inData;
  969.     newHeight = (*desc)->height;
  970.     newWidth = (*desc)->width;
  971.     stripBytes = ((newWidth+1)>>1) * 5;            /* the number of bytes in a strip (2-scanlines/strip) */
  972.     
  973.     /* figure out how many 2x2 blocks we want to strip from each side of the image */
  974.  
  975.     trimOffTop = rect.top>>1;
  976.     trimOffBottom  = (newHeight - rect.bottom) >> 1;
  977.     trimOffLeft = rect.left>>1;
  978.     trimOffRight  = (newWidth - rect.right) >> 1;
  979.  
  980.     /* point to the start of the first strip we are using */
  981.  
  982.     startP  = dataP + stripBytes * trimOffTop;
  983.  
  984.  
  985.     /* make the trim values pixel based */
  986.     
  987.     trimOffLeft <<= 1;
  988.     trimOffTop <<= 1;
  989.     trimOffBottom <<= 1;
  990.     trimOffRight <<= 1;
  991.     
  992.     /* calculate new height and width */
  993.     
  994.     newHeight -= trimOffTop + trimOffBottom;
  995.     newWidth -=  trimOffLeft + trimOffRight;
  996.     
  997.     /* calc size in bytes of strips of the new width */
  998.     
  999.     newStripBytes = ((newWidth+1)>>1) * 5;        
  1000.  
  1001.     /* figure number of bytes to toss at the beginning of each strip  */
  1002.     
  1003.     bytesOffLeft = (trimOffLeft>>1) * 5;
  1004.  
  1005.     /* figure size of new trimmed image */
  1006.     
  1007.     size = newStripBytes * (newHeight>>1);
  1008.     
  1009.     /* make sure it's gonna fit */
  1010.     
  1011.     if ( size > outDataSize )  {
  1012.         result = codecErr;
  1013.         goto bail;
  1014.     }
  1015.         
  1016.     /* now go through the strips and copy the needed portion of each to the new data */
  1017.  
  1018.     if (  dataProc ) {
  1019.         short rightBytes = stripBytes - newStripBytes - bytesOffLeft;
  1020.         for ( y=0; y < trimOffTop; y++ ) {
  1021.             if ( (result=dataProc->dataProc(&inData,stripBytes,dataProc->dataRefCon)) != noErr )
  1022.                 goto bail;
  1023.             inData += stripBytes;
  1024.             if (progressProc ) {
  1025.                 if ( (result=progressProc->progressProc(codecProgressUpdatePercent,
  1026.                     FixDiv(y, (*desc)->height),progressProc->progressRefCon)) != noErr)  {
  1027.                     result = codecAbortErr;
  1028.                     goto bail;
  1029.                 }
  1030.             }
  1031.         }
  1032.         for ( y=0; y < newHeight; y+= 2) {
  1033.             if ( bytesOffLeft ) {
  1034.                 if ( (result=dataProc->dataProc(&inData,bytesOffLeft,dataProc->dataRefCon)) != noErr )
  1035.                     goto bail;
  1036.                 inData += bytesOffLeft;
  1037.             }
  1038.             if ( (result=dataProc->dataProc(&inData,newStripBytes,dataProc->dataRefCon)) != noErr )
  1039.                 goto bail;
  1040.             if (  flushProc ) {
  1041.                 if ( (result=flushProc->flushProc(inData,newStripBytes,flushProc->flushRefCon)) != noErr ) {
  1042.                     result = codecSpoolErr;
  1043.                     goto bail;
  1044.                 }
  1045.             }
  1046.             else {
  1047.                 BlockMove(inData,outData,newStripBytes);
  1048.                 outData += newStripBytes;
  1049.             }
  1050.             inData += newStripBytes;
  1051.             if ( rightBytes ) {
  1052.                 if ( (result=dataProc->dataProc(&inData,rightBytes,dataProc->dataRefCon)) != noErr ) {
  1053.                     result = codecSpoolErr;
  1054.                     goto bail;
  1055.                 }
  1056.                 inData += rightBytes;
  1057.             }
  1058.             if (progressProc) {
  1059.                 if ( (result=progressProc->progressProc(codecProgressUpdatePercent,
  1060.                     FixDiv((trimOffTop + y),(*desc)->height),progressProc->progressRefCon)) != noErr ) {
  1061.                     result = codecAbortErr;
  1062.                     goto bail;
  1063.                 }
  1064.             }
  1065.         }
  1066.     }
  1067.     else {
  1068.         inData += stripBytes * trimOffTop;
  1069.         for ( y=0; y < newHeight; y += 2, inData += stripBytes) {
  1070.             if (  flushProc ) {
  1071.                 if ( (result=flushProc->flushProc(inData + bytesOffLeft,newStripBytes,flushProc->flushRefCon)) != noErr ) {
  1072.                     result = codecSpoolErr;
  1073.                     goto bail;
  1074.                 }
  1075.             }
  1076.             else {
  1077.                 BlockMove(inData + bytesOffLeft,outData,newStripBytes);
  1078.                 outData += newStripBytes;
  1079.             }
  1080.             if (progressProc ) {
  1081.                 if ( (result=progressProc->progressProc(codecProgressUpdatePercent,
  1082.                     FixDiv((trimOffTop + y),(*desc)->height),progressProc->progressRefCon)) != noErr ) {
  1083.                     result = codecAbortErr;
  1084.                     goto bail;
  1085.                 }
  1086.             }
  1087.         }
  1088.     }
  1089.  
  1090.     /* adjust the rectangle to reflect our changes */
  1091.     
  1092.     trimRect->top -= trimOffTop;
  1093.     trimRect->bottom -= trimOffTop;
  1094.     trimRect->left -= trimOffLeft;
  1095.     trimRect->right -= trimOffLeft;
  1096.  
  1097.     /* return the new width and height in the image description and the size */
  1098.     
  1099.     (*desc)->height = newHeight;
  1100.     (*desc)->width = newWidth;
  1101.     (*desc)->dataSize = size;
  1102. bail:
  1103.     if ( progressProc ) 
  1104.         progressProc->progressProc(codecProgressClose,0,progressProc->progressRefCon);
  1105.  
  1106.     return(result);
  1107.  
  1108.  
  1109. }
  1110.  
  1111.     
  1112. #ifndef    HAS_ASM        /* we could do this part in assembly for speed if we desired */
  1113.  
  1114.  
  1115. #define    READPIXEL(n)                \
  1116.     l = *lp++;                        \
  1117.     r = (l>>16);                    \
  1118.     g = (l>>8);                        \
  1119.     b = l;                            \
  1120.     yt = (R_W*r + G_W*g + B_W*b);    \
  1121.     if ( yt > ((256L<<16)-1) ) yt = ((256L<<16)-1); \
  1122.     ys[n] = yt>>16;                    \
  1123.     y += yt;                        \
  1124.     u += r;                            \
  1125.     v += b;                        
  1126.  
  1127. #define    READPIXEL_TAB(n)                \
  1128.     l = *lp++;                        \
  1129.     r = (l>>16);                    \
  1130.     g = (l>>8);                        \
  1131.     b = l;                            \
  1132.     yt = (rwTable[r] + gwTable[g] + bwTable[b]);    \
  1133.     if ( yt > ((256L<<16)-1) ) yt = ((256L<<16)-1); \
  1134.     ys[n] = yt>>16;                    \
  1135.     y += yt;                        \
  1136.     u += r;                            \
  1137.     v += b;                        
  1138.  
  1139.  
  1140. pascal void
  1141. CompressStrip(char *data,char *baseAddr,short rowBytes,short len,SharedGlobals *sg)
  1142. {
  1143.  
  1144.     register long    l,*lp = (long *)baseAddr;
  1145.     register unsigned char     r,g,b;
  1146.     unsigned char    ys[4];
  1147.     register long    y,yt;
  1148.     short    u,v;
  1149.     short    rowLongs = (rowBytes>>2);
  1150.         
  1151.         
  1152.     
  1153.     
  1154.     len++;
  1155.     len>>=1;
  1156.     
  1157.     if ( sg->rgbwTable && *sg->rgbwTable  ) {
  1158.         long    *rwTable,*gwTable,*bwTable;
  1159.     
  1160.         rwTable = (long *)*sg->rgbwTable;
  1161.         gwTable = rwTable + 256;
  1162.         bwTable = rwTable + 512;
  1163.  
  1164.         while ( len-- > 0) {
  1165.             y = u = v = 0;
  1166.             READPIXEL_TAB(0);
  1167.             READPIXEL_TAB(1);
  1168.             lp += rowLongs-2;
  1169.             READPIXEL_TAB(2);
  1170.             READPIXEL_TAB(3);
  1171.             lp -= rowLongs;
  1172.         
  1173.             y >>= 16;
  1174.             u = (u - y)>>4;
  1175.             v = (v - y)>>4;
  1176.             
  1177.             l =  (long)(0xfc & (ys[0])) << 24;
  1178.             l |= (long)(0xfc & (ys[1])) << 18;
  1179.             l |= (long)(0xfc & (ys[2])) << 12;
  1180.             l |= (long)(0xfc & (ys[3])) <<  6;
  1181.             l |= u & 0xff;
  1182.             *(long *)data = l;
  1183.             data += sizeof(long);
  1184.             *data++ = v;
  1185.         }
  1186.     } else {
  1187.         while ( len-- > 0) {
  1188.             y = u = v = 0;
  1189.             READPIXEL(0);
  1190.             READPIXEL(1);
  1191.             lp += rowLongs-2;
  1192.             READPIXEL(2);
  1193.             READPIXEL(3);
  1194.             lp -= rowLongs;
  1195.         
  1196.             y >>= 16;
  1197.             u = (u - y)>>4;
  1198.             v = (v - y)>>4;
  1199.             
  1200.             l =  (long)(0xfc & (ys[0])) << 24;
  1201.             l |= (long)(0xfc & (ys[1])) << 18;
  1202.             l |= (long)(0xfc & (ys[2])) << 12;
  1203.             l |= (long)(0xfc & (ys[3])) <<  6;
  1204.             l |= u & 0xff;
  1205.             *(long *)data = l;
  1206.             data += sizeof(long);
  1207.             *data++ = v;
  1208.         }
  1209.     }
  1210. }
  1211.  
  1212.  
  1213.  
  1214.  
  1215. #define    WRITEPIXEL                \
  1216.     r = PIN(u+y);                \
  1217.     b = PIN(v+y);                \
  1218.     y <<= 16;                    \
  1219.     y -= r * R_W;                \
  1220.     y -= b * B_W;                \
  1221.     g = PIN(y / G_W);            \
  1222.     *lp++ = (long) ( (long) r <<16) | ( (long) g <<8) | b;    
  1223.  
  1224. #define    WRITEPIXEL_TAB            \
  1225.     r = PIN(u+y);                \
  1226.     b = PIN(v+y);                \
  1227.     y <<= 16;                    \
  1228.     y -= rwTable[r];        \
  1229.     y -= bwTable[b];        \
  1230.     g = giwTable[PIN(y>>16)];    \
  1231.     *lp++ = (long) ( (long) r <<16) | ( (long) g <<8) | b;    
  1232.     
  1233. pascal void
  1234. DecompressStrip(char *data,char *baseAddr,short rowBytes,short len,SharedGlobals *sg)
  1235. {
  1236.     register long    y;
  1237.     register unsigned char     r,g,b;
  1238.     register long    l,*lp;
  1239.     long     u,v;
  1240.     unsigned char    ys[4];
  1241.     short    rowLongs = (rowBytes>>2);
  1242.     short    blen = len;
  1243.  
  1244.     lp = (long *)baseAddr;
  1245.     blen++;
  1246.     blen >>= 1;
  1247.     
  1248.     if ( sg->rgbwTable && *sg->rgbwTable &&  sg->giwTable && *sg->giwTable ) {
  1249.         unsigned char    *giwTable;    
  1250.         long    *rwTable,*bwTable;
  1251.  
  1252.         giwTable = *sg->giwTable;
  1253.         rwTable = (long *)*sg->rgbwTable;
  1254.         bwTable = rwTable + 512;
  1255.         while ( blen-- > 0 ) {
  1256.             l = *(long *)data;
  1257.             data += sizeof(long);
  1258.             ys[0] = (0xfc & (l>>24));
  1259.             ys[1] = (0xfc & (l>>18));
  1260.             ys[2] = (0xfc & (l>>12));
  1261.             ys[3] = (0xfc & (l>>6));
  1262.             u = (char)l;
  1263.             v = *data++;
  1264.             u<<=2;
  1265.             v<<=2;
  1266.             y = ys[0];
  1267.             WRITEPIXEL_TAB;
  1268.             y = ys[1];
  1269.             WRITEPIXEL_TAB;
  1270.             lp += rowLongs - 2;
  1271.             y = ys[2];
  1272.             WRITEPIXEL_TAB;
  1273.             y = ys[3];
  1274.             WRITEPIXEL_TAB;
  1275.             lp -= rowLongs;
  1276.         }
  1277.     } else {
  1278.         while ( blen-- > 0 ) {
  1279.             l = *(long *)data;
  1280.             data += sizeof(long);
  1281.             ys[0] = (0xfc & (l>>24));
  1282.             ys[1] = (0xfc & (l>>18));
  1283.             ys[2] = (0xfc & (l>>12));
  1284.             ys[3] = (0xfc & (l>>6));
  1285.             u = (char)l;
  1286.             v = *data++;
  1287.             u<<=2;
  1288.             v<<=2;
  1289.             y = ys[0];
  1290.             WRITEPIXEL;
  1291.             y = ys[1];
  1292.             WRITEPIXEL;
  1293.             lp += rowLongs - 2;
  1294.             y = ys[2];
  1295.             WRITEPIXEL;
  1296.             y = ys[3];
  1297.             WRITEPIXEL;
  1298.             lp -= rowLongs;
  1299.         }
  1300.     }
  1301. }
  1302.  
  1303.  
  1304. #endif
  1305.  
  1306.  
  1307.